/**
 * 
 */
package ken.app.xmlhandler;

import java.util.ArrayList;
import java.util.List;

import ken.app.bean.TreeAllBean;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

/**
 * @author ken.chung
 * 
 */
public class ShareBabyListXMLHandler extends DefaultHandler {
	
	/* 變數宣告 */
	private boolean in_item = false;
	private boolean in_result = false;
	
	private List<Integer> list;
	private TreeAllBean mTreeInfo;
	private String title = "";
	private StringBuffer buf = new StringBuffer();

	/* 將轉換成List<News>的XML資料回傳 */
	public List<Integer> getParsedData() {
		return list;
	}

	/* 將解析出的RSS title回傳 */
	public String getRssTitle() {
		return title;
	}

	/* XML文件開始解析時呼叫此method */
	@Override
	public void startDocument() throws SAXException {
		if (list == null) {
			list = new ArrayList<Integer>();
		}
	}

	/* XML文件結束解析時呼叫此method */
	@Override
	public void endDocument() throws SAXException {
	}

	/* 解析到Element的開頭時呼叫此method */
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		
			if (localName.equals("item")) {
				this.in_item = true;
				/* 解析到item的開頭時new一個News物件 */
			}
		buf.setLength(0);
	}

	/* 解析到Element的結尾時呼叫此method */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (localName.equals("item")) {
			this.in_item = false;
			/* 解析到item的結尾時將News物件寫入List中 */
			list.add(Integer.valueOf(buf.toString().trim()));
		}
	}

	/* 取得Element的開頭結尾中間夾的字串 */
	@Override
	public void characters(char ch[], int start, int length) {
		if (this.in_item) {
			/* 將char[]加入StringBuffer */
			buf.append(ch, start, length);
		}
	}
}